<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="校区" prop="campusId">
        <el-select
          @change="changeCampus"
          v-model="queryParams.campusId" placeholder="选择校区" clearable @keyup.enter.native="handleQuery">
          <el-option v-for="item in dict.type.campus_type"
                     :value="parseInt(item.value)" :label="item.label" :key="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="学科" prop="subjectId">
        <el-select
          @change="changeSubject"
          v-model="queryParams.subjectId" placeholder="选择学科" clearable
          @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in dict.type.discipline_type"
            :value="parseInt(item.value)"
            :label="item.label" :key="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          @change="changeTeacherType"
          v-model="queryParams.type" placeholder="选择老师类型" clearable
          @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in dict.type.teacher_tpye"
            :value="parseInt(item.value)"
            :label="item.label"
            :key="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="老师" prop="teacherId">
        <el-select filterable v-model="queryParams.teacherId" placeholder="选择老师" clearable>
          <el-option
            v-for="teacher in teachers"
            :key="teacher.id"
            :label="teacher.name"
            :value="teacher.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="day">
        <el-date-picker clearable
                        style="width: 200px !important;"
                        v-model="queryParams.startDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择日期">
        </el-date-picker>
        -
        <el-date-picker clearable
                        style="width: 200px !important;"
                        v-model="queryParams.endDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="排班类型" prop="resteringType">
        <el-select v-model="queryParams.resteringType" placeholder="请选择排班类型" clearable>
          <el-option
            v-for="dict in dict.type.scheduling_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['eduadmin:scheduling:export']"
        >导出
        </el-button>
      </el-form-item>
    </el-form>

    <el-calendar v-loading="loading">
      <!-- 这里使用的是 2.5 slot 语法，对于新项目请使用 2.6 slot 语法-->
      <template slot="dateCell" slot-scope="{date, data}">
        <div style="min-height: 50px;height: auto;text-align: left" @click="pbChange(data.day)">
          <p :class="data.isSelected ? 'is-selected' : ''">
            {{ data.day.split('-').slice(1).join('-') }}
            <el-button type="success" plain size="mini" @click="cellClick(date,data)">排班</el-button>
            <!--        <el-checkbox v-model="data.isSelected">排班</el-checkbox>-->
          </p>
          <span
            v-for="(item, index) in scheduleData"
            v-if="item.day.indexOf(data.day) != -1 && currentTeacherId == item.teacherId"
          >
              </span>
          <el-tag v-for="(item, index) in scheduleData"
                  style="margin-top: -50px"
                  v-if="item.day.indexOf(data.day) != -1 "
                  slot="reference">{{ item.name }}
          </el-tag>

        </div>
      </template>
    </el-calendar>

    <el-dialog :title="title" :visible.sync="schedulingOpen" width="500px" append-to-body>
      <el-form ref="rform" :model="rform" :rules="rules" label-width="80px">
        <el-form-item label="考勤组" prop="groupId">
          <el-select v-model="rform.groupId" placeholder="请选择考勤组">
            <el-option
              v-for="dict in dingGroups"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班次选择" prop="schedulingType">
          <el-select v-model="rform.resteringType" placeholder="请选择班次">
            <el-option
              v-for="dict in dingClass"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button type="danger" v-if="rform.id" @click="submitDelete">删除排班</el-button>
        <el-button @click="schedulingOpen = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listScheduling,
  delScheduling,
  addScheduling,
  listGroupForCurrent,
  listClasssForCurrent,
  getTeacher,
  // getSchedulingByDate
} from "@/api/teacher/scheduling";

import {listTeacher} from "@/api/teacher/teacher";

export default {
  name: "Teacher",
  dicts: ['campus_type', 'discipline_type', 'teacher_tpye', 'scheduling_type'],
  data() {
    return {
      loading: false,
      currentTeacherId: 51,
      dingClass: [],
      dingGroups: [],
      title: "选择排班",
      downCount: 0,
      teachers: [],
      teacherName :[],
      showSearch: true,
      queryParams: {
        pageNum: -1,
        campusId: null,
        subjectId: null,
        startDate: this.parseTime(new Date(), "{y}-{m}") + "-1"
      },
      rform: {
        resteringType: null,
        groupId: null,
      },
      schedulingOpen: false,
      rules: {
        status: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
      },
      scheduleData: [],
    };
  },
  computed: {
    checked(data) {

    },
  },
  created() {
    this.getList();
    this.getTeachersList();
    this.listDingClass();
    this.listDingGroup();
    this.getCurrentTeacher();
  },
  methods: {
    getCurrentTeacher() {
      getTeacher().then(response => {
        this.currentTeacherId = response.data;
      })
    },


    // 直接点击排班
    pbChange(day) {
      this.loading = true;
      addScheduling({day: day,teacherId : this.currentTeacherId}).then(response => {
        this.getList();
        this.loading = false;
      }).catch(reason => {
        this.$modal.msgError("排班失败");
        this.loading = false;
      })
    },


    listDingClass() {
      listClasssForCurrent({pageNum: -1}).then(res => {
        this.dingClass = res.data;
      });
    },
    listDingGroup() {
      listGroupForCurrent({pageNum: -1}).then(res => {
        this.dingGroups = res.data;
      });
    },
    changeTeacherType(type) {
      this.queryParams.type = type;
      this.getTeachersList();
    },
    changeSubject(subjectId) {
      this.queryParams.subjectId = subjectId;
      this.getTeachersList();
    },
    changeCampus(campusId) {
      this.queryParams.campusId = campusId;
      this.getTeachersList();
    },
    getTeachersList(campusId) {
      this.queryParams.pageNum = -1;
      listTeacher(this.queryParams).then(response => {
        this.teachers = response.rows;
      });
    },
    handleQuery() {
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      this.loading = true;
      this.queryParams.pageNum = -1;
      listScheduling(this.queryParams).then(response => {
        this.scheduleData = response.rows;
        this.teacherName = response.rows
        console.log(this.teacherName)
        this.loading = false;
      }).catch(reason => {
        this.$modal.msgError("添加失败");
        this.loading = false;
      })
    },
    // 点击排班 弹出模态框
    cellClick(date, data) {
      let day = data.day;
      this.rform.day = day;
      var _this = this;
      _this.title = "新增排班"
      _this.rform.id = null;
      _this.rform.resteringType = null;
      _this.rform.groupId = null;
      this.schedulingOpen = true;

      // // 掉接口查询考勤组
      // getSchedulingByDate(day).then(response => {
      //   if (response.data) {
      //     _this.rform.resteringType = parseInt(response.data.resteringType);
      //     _this.rform.groupId = parseInt(response.data.groupId);
      //     _this.rform.id = response.data.id;
      //     _this.title = response.data.teacherName + ":" + day;
      //   }
      //
      // });

    },
    submitDelete() {
      if (this.rform.id) {
        delScheduling(this.rform.id).then(response => {
          this.$modal.msgSuccess("删除成功");
          this.schedulingOpen = false;
          this.getList();
        });
      }

    },
    submitForm() {
      // if(this.rform.resteringType==468530879||this.rform.resteringType==943405022||this.rform.resteringType==460685857){
      //     if(!this.$store.getters.roles.includes("admin")){
      //       this.$message.error("无权选择此类型");
      //     }
      //     return;
      // }
      this.loading = true;
      addScheduling(this.rform).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.schedulingOpen = false;
        this.getList();
        this.loading = false;
      });
    },
    handleExport() {
      this.downExcel();
    },
    // 下载excel表格
    downExcel() {
      var _this = this;
      if (_this.downCount >= _this.teachers.length) return;
      let tname = _this.teachers[_this.downCount].name;
      var param = {
        teacherId: this.teachers[_this.downCount].id
      }
      setTimeout(function () {
        _this.download('eduadmin/scheduling/export', param, '排班表-' + tname + '.xls')
        _this.downCount++;
        _this.downExcel();
      }, 1000);
    }
  }
};
</script>

<style>
.el-calendar-table td.is-today p {
  color: #d53737;
  font-size: 18px;
  text-align: left;
}

.el-tag {
  margin: 1px;
}

.is-selected {
  color: #78aef8;
}

p {
  margin: 10px auto;
}

ul {
  list-style: none;
  padding: 0;
}

.importantSty {
  color: red;
  /*background-color: rgba(235, 22, 22, 0.5);*/
}

.secondarySty {
  color: tan;
  /*background-color: rgba(235, 150, 22, 0.5);*/
}

.el-calendar-day {
  height: auto !important;
  min-height: 50px;
  padding: 2px !important;
}

.el-tag--medium {
  height: 22px;
  line-height: 22px;
  margin-top: 1px;
}

</style>
